package initialize

import (
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"report/global"
	"strconv"
)

func RegisterNacos() {
	// 创建clientConfig
	clientConfig := constant.ClientConfig{
		NamespaceId:         "4917be47-b2de-4f2d-af81-01c64df1db5d", // 替换成你的命名空间
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "./log",
		CacheDir:            "./cache",
		BeatInterval:        5000,
		LogLevel:            "debug",
	}

	// 至少一个ServerConfig
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      global.GVA_CONFIG.Nacos.Addr,
			ContextPath: "/nacos",
			Port:        uint64(global.GVA_CONFIG.Nacos.Port),
			Scheme:      "http",
		},
	}

	// 创建服务发现客户端 (推荐)
	namingClient, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)

	if err != nil {
		fmt.Println("clients.NewNamingClient err,", err)
	}
	num, err := strconv.ParseUint(global.GVA_CONFIG.Grpc.Addr, 10, 64)
	success, err := namingClient.RegisterInstance(vo.RegisterInstanceParam{
		Ip:          "172.17.16.13",
		Port:        num,
		ServiceName: "report.go",
		Weight:      1,
		Enable:      true,
		Healthy:     true,
		Ephemeral:   true,
		Metadata:    map[string]string{"idc": "shanghai"},
		ClusterName: "DEFAULT",       // 默认值DEFAULT
		GroupName:   "DEFAULT_GROUP", // 默认值DEFAULT_GROUP
	})
	if !success {
		return
	} else {
		fmt.Println("namingClient.RegisterInstance Success")
	}
}
